package com.lh.consume;

import com.alibaba.dubbo.rpc.service.GenericService;
import com.sise.bean.ComputerDTO;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：idea
 * 日期：2018/8/27
 * 描述：泛化类型(该组件比较偏向于重量级，不建议经常使用)
 */
public class DubboGenericService {

    //基本类型参数的远程调用
    private static void generationServiceRef(ClassPathXmlApplicationContext context){
        GenericService genericService=(GenericService) context.getBean("computerRpcService2");
        //方法对象
        ComputerDTO computerDTO=new ComputerDTO();
        computerDTO.setId(1001);
        computerDTO.setName("name");
        Object resultObj=genericService.$invoke("save",new String[]{"com.sise.bean.ComputerDTO"},new Object[]{computerDTO});
    }

    //通过hashmap类型进行参数调用
    private static ComputerDTO findOne(ClassPathXmlApplicationContext context){
        GenericService genericService=(GenericService) context.getBean("computerRpcService2");
//        Map<String,Object> map=new HashMap<>();
//        map.put("id",1001);
//        map.put("name","autho sss");
        ComputerDTO computerDTO=new ComputerDTO();
        computerDTO.setId(1001);
        computerDTO.setName("name");
        Object result=genericService.$invoke("findOne",new String[]{"com.sise.bean.ComputerDTO"},
                new Object[]{computerDTO});
        return (ComputerDTO) result;
    }

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(
                new String[]{"classpath:application-consumer.xml"});
        //远程调用的服务接口
//        generationServiceRef(context);
        //远程调用获取电脑dto对象
        ComputerDTO computerDTO=findOne(context);
        System.out.println(computerDTO.toString());
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
